<% layout("/inc/layout_page.html"){ %>

<form action="${BASE}/admin/AdminUser/save" id="form_save" method="post">
<input type="hidden" id="id" name="id" value="${obj.id}">
<input type="hidden" id="sid" name="sid" value="${obj.sid}">
<input type="hidden" id="roleSids" name="roleSids" value="${obj.roleSids}">
<input type="hidden" id="roleNames" name="roleNames" value="${obj.roleNames}">
<div class="box">
  <div class="box-header">
    <h4 class="box-title"><i class="fa fa-bars"></i> 修改管理员信息</h4>
    <div class="box-tools">
      <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-save"></i> 保存</button>
      <a href="javascript:;" onclick="layer_close()" class="btn btn-info btn-sm"><i class="fa fa-chevron-left"></i> 返回</a>
    </div>
  </div>
  <div class="box-body">
    <table class="table tform table-bordered">

      <tr>
        <th width="20%"><label for="name"><i class="required">*</i>登录名：</label></th>
        <td width="30%"><input id="name" name="name" value="${obj.name}" type="text" class="form-control"></td>
        <th width="20%"><label for="realName">真实姓名：</label></th>
        <td width="30%"><input id="realName" name="realName" value="${obj.realName}" type="text" class="form-control"></td>
      </tr>
      <tr>
        <th><label for="password">密码：</label></th>
        <td><a href="javascript:;" id="a_btn_changepass" class="btn btn-primary btn-sm"><i class="fa fa-key"></i> 重新设置密码</a></td>
        <td colspan="2">如果某管理员忘记密码，超级管理员可以通过这里重置该管理员密码</td>
      </tr>
      <tr>
        <th><label for="sex">性别：</label></th>
        <td>
          <label class="control-label"><input type="radio" name="sex" class="minimal" value="1" ${obj.sex==1?"checked"}> 男</label>
          <label class="control-label"><input type="radio" name="sex" class="minimal" value="0" ${obj.sex==0?"checked"}> 女</label>
        </td>
        <th><label for="email"><i class="required">*</i>邮箱：</label></th>
        <td><input id="email" name="email" value="${obj.email}" type="text" class="form-control"></td>
      </tr>
      <tr>
        <th><label for="mobile"><i class="required">*</i>手机：</label></th>
        <td><input id="mobile" name="mobile" value="${obj.mobile}" type="text" class="form-control"></td>
        <th><label for="phone">电话：</label></th>
        <td><input id="phone" name="phone" value="${obj.phone}" type="text" class="form-control"></td>
      </tr>
      <tr>
        <th><label>选择角色：</label></th>
        <td colspan="3" id="td_radio_role_list"></td>
      </tr>
      
      <tr>
        <th><label for="remarks">备注信息：</label></th>
        <td>
          <textarea id="remarks" name="remarks" cols="" rows="" class="form-control"  placeholder="备注信息...255个字符以内" dragonfly="true" onKeyUp="textarealength(this,255)">${obj.remarks}</textarea>
          <p class="textarea-numberbar"><em class="textarea-length">0</em>/255</p>
        </td>
      </tr>
    </table>
  </div>
  <div class="box-footer">
    <div class="box-tools pull-right">
      <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-save"></i> 保存</button>
      <a href="javascript:;" onclick="layer_close()" class="btn btn-info btn-sm"><i class="fa fa-chevron-left"></i> 返回</a>
    </div>
  </div>
</div>
</form>

<script type="text/javascript">
function loadRoles(){
	$.getJSON("${BASE}/admin/AdminRole/listAllWithChecked",{roleSids:"${obj.roleSids}"},function(jsondata){
		if(jsondata.success){
			var td = $("#td_radio_role_list");
			var roles = jsondata.data;
			for(var i=0;i<roles.length;i++){
				var html = '<label class="control-label">&nbsp;<input type="checkbox" name="roleIds" class="minimal" data-sid="'+roles[i].sid+'" data-name="'+roles[i].name+'" value="'+roles[i].id+'" ';
				if(roles[i].tails.isChecked)
					html += ' checked ';
				html += ' > '+roles[i].name+'&nbsp;&nbsp;</label>';
				td.append(html);
			}
			rCheck();

			$("#form_save input[name='roleIds']").on('ifChanged', function(event){
				var sids = "";
				var names = "";
				$("#form_save input[name='roleIds']:checkbox").each(function(){
					if(true == $(this).is(':checked')){
						sids+=","+$(this).attr("data-sid");
						names+=","+$(this).attr("data-name");
					}
				});
				$("#roleSids").val(sids);
				$("#roleNames").val(names);
			});
		}
	});
}

function rCheck(){
    $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
        checkboxClass: 'icheckbox_minimal-blue',
        radioClass: 'iradio_minimal-blue'
    });
}

$(function(){

	rCheck();
	
	$("#a_btn_changepass").click(function(){
	    var index = layer.open({
			type: 2,
			closeBtn: 1,
			title: "重置登录密码",
			area: ['80%', '80%'],
			content: "${BASE}/admin/AdminUser/changepass?sid=${obj.sid}"
		});
	});
	
    loadRoles(); 
	
	$("#form_save").validate({
		errorPlacement: function(error, element) {
			error.appendTo(element.parent());
		},
		submitHandler : function(form) {
			layer.load(1, {shade: [0.3,'#fff']});
			$(form).ajaxSubmit({
				dataType:"json",
				success:function( jsondata ){
					layer.closeAll('loading');
					if( jsondata.success){
						parent.toastr.success('保存成功！');
						parent.reload_table();
						layer_close();
					}else{
						parent.toastr.error(jsondata.msg);
					}
				}
			});
		},
		rules : {
			name : {
				required : true,
				remote: {
				    url: "${BASE}/admin/AdminUser/otherLoginUsernameIsHas",     //后台处理程序
				    type: "post",               //数据发送方式
				    data: {                     //要传递的数据
				    	name: function() {
				            return $("#name").val();
				        },
				    	sid: function() {
				            return $("#sid").val();
				        }
				    } 
				}
			},
			email : {
				required : true,
				email : true
			},
			mobile : {
				required : true,
			}
		}, 
		messages : { 
			name : { 
				required : "登录名不能为空",
				remote : "该登录名已经被使用"
			},
			email : {
				required : "邮箱不能为空",
				email : "邮箱格式不正确"
			},
			mobile : {
				required : "手机号码不能为空",
			}
		}
	});
});
</script>
<% } %>